package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.*;
import com.baitengsoft.patentapimonitor.api.service.RptApplicantService;
import com.baitengsoft.patentapimonitor.api.service.RptPatentDataAreaService;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 统计分析
 *
 * @author Chen 2017/12/25
 */
@RestController
@RequestMapping("/api/v1/web/analysis")
@Api(value = "前台-统计分析", description = "统计分析")
public class WebStatisticAnalysisController extends AbstractController {
    @Autowired
    private RptPatentDataAreaService rptPatentDataAreaService;

    /**
     * 历年申请趋势
     */
    @ApiOperation(value = "历年申请趋势汇总/type 0.申请趋势,1 授权趋势")
    @GetMapping("/trendanalysis/{type}")
    @ResponseBody
    public Res geTrendAnalysisList(@PathVariable(value = "type") Integer type) throws Exception {
        if (type == null) {
            return Res.error("参数有误");
        }
        if (type != 0 && type != 1) {
            return Res.error("趋势参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        List<WebYearStatisticInfo> infos = rptPatentDataAreaService.getTrendAnalysisList(userInfo.getPlatformid(), type);
        return Res.ok().put("infos", infos);
    }

    /**
     * 授权专利月度统计
     */
    @ApiOperation(value = "授权专利月度统计/type 0.申请趋势,1 授权趋势")
    @GetMapping("/trenddetail/{type}")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "year", value = "年份", required = true),
    })
    public Res getTrendDetailList(@PathVariable(value = "type") Integer type, Integer year) throws Exception {
        if (type == null || year == null) {
            return Res.error("参数有误");
        }
        if (type != 0 && type != 1) {
            return Res.error("趋势参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        List<WebYearStatisticInfo> infos = rptPatentDataAreaService.getTrendDetailList(userInfo.getPlatformid(), type, year);
        return Res.ok().put("infos", infos);
    }

    /**
     * 有效专利区域统计
     */
    @ApiOperation(value = "有效专利区域统计")
    @GetMapping("/areaAnalysis")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false)
    })
    public Res getAreaAnalysislList(String order, String sort) throws Exception {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        List<String> sorts = Arrays.asList("fmcount", "syxxcount", "wgsjcount", "total");
        if (StringUtils.isNotBlank(order)) {
            if ((!order.equals("desc")) && (!order.equals("asc")))
                return Res.error("排序参数有误");
        }
        if (StringUtils.isNotBlank(sort)) {
            if (!sorts.contains(sort)) {
                return Res.error("排序参数有误");
            }
        }
        List<WebAreaStatisticInfo> infos = rptPatentDataAreaService.getAreaAnalysislList(userInfo.getPlatformid(), order, sort);
        return Res.ok().put("infos", infos);
    }

    /**
     * 有效专利区域统计
     */
    @ApiOperation(value = "有效专利区域统计")
    @GetMapping("/areaAnalysis/all")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false)
    })
    public Res getAreaAnalysisAllList(String order, String sort) throws Exception {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        List<String> sorts = Arrays.asList("fmcount", "syxxcount", "wgsjcount", "total");
        if (StringUtils.isNotBlank(order)) {
            if ((!order.equals("desc")) && (!order.equals("asc")))
                return Res.error("排序参数有误");
        }
        if (StringUtils.isNotBlank(sort)) {
            if (!sorts.contains(sort)) {
                return Res.error("排序参数有误");
            }
        }
        List<WebAreaStatisticInfo> infos = rptPatentDataAreaService.getAreaAnalysisAllList(userInfo.getPlatformid(), order, sort);
        return Res.ok().put("infos", infos);
    }

    /**
     * IPC分类统计
     */
    @ApiOperation(value = "IPC分类统计")
    @GetMapping("/ipcAnalysis")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列 ", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false)
    })
    public Res getIPCAnalysislList(String order, String sort) throws Exception {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        if (StringUtils.isNotBlank(order) && StringUtils.isNotBlank(sort)) {
            if (!WebUtils.checkSortParam(sort, order)) return Res.error("排序参数有误");
        }
        WebUtils.checkSortParam(sort, order);
        List<WebIPCAnalysisInfo> infos = rptPatentDataAreaService.getIPCAnalysisList(userInfo.getPlatformid(), order, sort);
        return Res.ok().put("infos", infos);
    }

    /**
     * 发明人统计
     */
    @ApiOperation(value = "发明人统计")
    @GetMapping("/inventorAnalysis")
    @ResponseBody
    public Res getInventorAnalysislList() throws Exception {
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        List<WebInventorAnalysisInfo> infos = rptPatentDataAreaService.getInventorAnalysislList(userInfo.getPlatformid());
        return Res.ok().put("infos", infos);

    }


    /**
     * 代理机构统计
     */
    @ApiOperation(value = "代理机构统计 1当前有效量，2专利总量，3今年公开量，4今年授权量")
    @GetMapping("/AgencyAnalysis/{type}")
    @ResponseBody
    public Res getAgencyAnalysislList(@PathVariable(value = "type") Integer type) throws Exception {
        if (type == null) {
            return Res.error("请求参数有误");
        }
        if (type != 1 && type != 2 && type != 3 && type != 4) {
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        List<WebAgencyAnalysisInfo> infos = rptPatentDataAreaService.getAgencyAnalysislList(userInfo.getPlatformid(), type);
        return Res.ok().put("infos", infos);
    }


}
